<template>
  <div class="shopping">
    <div class="headImg">
      <img src="@/assets/img/jiaju.jpg" alt="图片">
    </div>
    <div class="shopContent">
      <div class="shopRanking">
        <div class="shopRankingHead"><!--宝贝排行榜-->{{ $t('shop.babyList') }}</div>
        <ul>
          <li v-for="item in cowryData" :key="item.fnumber" @click="pushDetail(item.fnumber)">
            <div class="rankingLeft"><img :src="item.fimg"></div>
            <div class="rankingRight">
              <div class="rightTop">
                {{ item.fname }}
              </div>
              <div class="rightTop">
                <!-- 编码-->{{ $t('shop.fnumber') }}: {{ item.fnumber }}
              </div>
              <div class="rankRigFont">￥{{ item.fprice }}</div>
              <div class="rightBom">
                <!--  已售出-->{{ $t('shop.sell') }}
                <span style="color: #666">{{ item.fqty || 0 }}</span>
                <!--笔-->{{ $t('shop.pen') }}
              </div>
              <div class="hoverModel">
                <!--                <el-button type="danger" size="mini" @click="shoppingTrolley(item)">加入购物车</el-button>-->
                <i class="el-icon-shopping-cart-2" @click.stop="shoppingTrolley(item)" />
              </div>
            </div>
          </li>
        </ul>
      </div>
      <div class="shop">
        <div class="shopHead">
          <div class="shopHead-1"><!--热销产品-->{{ $t('shop.hotSale') }}</div>
          <div class="shopHead-2">
            <el-button size="mini" @click="standardList(1,'餐桌')"><!--餐桌-->{{ $t('shop.diningTable') }}</el-button>
            <el-button size="mini" @click="standardList(1,'餐椅')"><!--餐椅-->{{ $t('shop.diningChair') }}</el-button>
            <span style="margin-left: 20px"><!--价格-->{{ $t('shop.price') }}：</span>
            <el-input
              v-model.number="stprice"
              size="mini"
              @change="standard(2, stprice)"
            />
            -
            <el-input
              v-model.number="edprice"
              size="mini"
              @change="standard(3, edprice)"
            />
            <el-button
              style="margin-left: 20px"
              size="mini"
              type="primary"
              @click="standardList(1)"
            ><!--确定-->{{ $t('btn.confirm') }}</el-button>
          </div>
        </div>
        <ul>
          <li v-for="item in tableData" :key="item.fnumber" @click="pushDetail(item.fnumber)">
            <div class="commodity">
              <div class="commodimg">
                <img :src="item.fimg" alt="图片">
              </div>
              <div class="price">
                <div class="price1">
                  <span>￥</span>
                  {{ item.fprice }}
                </div>
                <!-- <div class="price2"><i class="el-icon-shopping-cart-2" /></div>-->
              </div>
              <div class="cont1">
                <!--编码-->{{ $t('shop.fnumber') }}: {{ item.fnumber }}
              </div>
              <div class="cont">
                {{ item.fname }}
              </div>
              <div class="footer">
                <span><!--总销量-->{{ $t('shop.totalSales') }}：</span>
                <span>{{ item.fqty }}</span>
              </div>
              <div class="hoverRB">
                <i class="el-icon-shopping-cart-2" @click.stop="shoppingTrolley(item)" />
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div class="fixRight">
      <div @click="pushView">
        <i class="el-icon-shopping-cart-2" />
        <br>
        <span><!--购物车-->{{ $t('shop.shopCart') }}</span>
      </div>
      <div v-if="scroTop" class="goTop" @click="scrollToTop">
        <i class="el-icon-arrow-up" />
        <br>
        <span><!--顶部-->{{ $t('shop.top') }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { queryMarketList, insertShopping, deleteMarket } from '@/api/marketManage/saleReport'
import { scrollTo } from '@/utils/scroll-to'
import Bus from '@/utils/bus'
export default {
  name: 'Shopping',
  data() {
    return {
      scroTop: false,
      tableData: [], // 热销
      cowryData: [], // 宝贝
      indexSelf: -1,
      pageNum: 1,
      stprice: '',
      edprice: '',
      roleName: '' // 权限
    }
  },
  created() {
    this.standardList()
    this.queryRankingList()
    this.roleName = this.$store.state.user.roleName[0]
    window.addEventListener('scroll', this.scrollHandle)
  },
  methods: {
    // 监听滚动条滚动
    scrollHandle(e) {
      const top = e.srcElement.scrollingElement.scrollTop
      top > 1000 ? this.scroTop = true : this.scroTop = false
      if (document.body.scrollHeight - top < 1200) {
        var vm = this
        clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
        vm.timer = setTimeout(() => {
          this.pageNum++
          this.standardList()
          this.queryRankingList()
        }, 100)
      }
    },
    // 滚动到顶部
    scrollToTop() {
      scrollTo(0, 300)
    },
    // 筛选数据
    standard(ev, en) {
      if (ev === 2) {
        this.stprice = en
      } else if (ev === 3) {
        this.edprice = en
      }
    },
    // 热销产品数据
    async standardList(ev, en) {
      if (ev === 1) {
        if (en) { this.fname = en }
        this.pageNum = 1
        this.tableData = []
      }
      const DATA = {
        fmarket: 'B',
        pageNum: this.pageNum,
        pageSize: 20,
        fname: this.fname,
        stprice: this.stprice,
        edprice: this.edprice
      }
      const { data: RES, code, message } = await queryMarketList(DATA)
      if (code === 0) {
        RES.array.length === 0
          ? window.removeEventListener('scroll', this.scrollHandle)
          : this.tableData.push(...RES.array)
      } else {
        this.$message.error(message)
      }
    },
    // 宝贝排行数据
    async queryRankingList() {
      const DATA = {
        fmarket: 'A',
        pageNum: this.pageNum,
        pageSize: 10
      }
      const { data: RES, code, message } = await queryMarketList(DATA)
      if (code === 0) {
        this.cowryData.push(...RES.array)
      } else {
        this.$message.error(message)
      }
    },
    previewImg(img, index, inde) {
      this.indexSelf = inde
      const item = this.tableData[index]
      this.$nextTick(() => {
        item.img = img
      })
    },
    // 加入购物车
    shoppingTrolley(ev) {
      const DATA = {
        fqty: 1,
        fprice: ev.fprice,
        fmaterialId: ev.fmaterialId
      }
      insertShopping(DATA).then(res => {
        if (res.code === 0) {
          // 触发购物车接口
          Bus.$emit('shopcart', 1)
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 删除商品
    delShopping(ev, index, ed) {
      const DATA = {
        fid: ev.fid
      }
      deleteMarket(DATA).then(res => {
        if (res.code === 0) {
          if (ed === 1) { // 宝贝
            this.cowryData.splice(index, 1)
          } else { // 热销
            this.tableData.splice(index, 1)
          }
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 跳转购物车
    pushView() {
      this.$router.push({ path: '/shopCart' })
    },
    pushDetail(fnumber) {
      this.$router.push({ path: `/productDetails/${fnumber}` })
    }
  }
}
</script>
<style lang="scss" scope>
.shopping {
  background: #f0f0f0;
  display: flex;
  flex-direction: column;
  align-items: center;
  .headImg {
    width: 100%;
    img {
      width: 100%;
      height: auto;
    }
  }
  .shopContent {
    display: flex;
    justify-content: flex-end;
    align-items: flex-start;
    width: 100%;

    .topRig {
      font-size: 12px;
      margin-right: 2px;
      z-index: 10;
    }
    .shopRanking {
      width: 20%;
      max-width: 200px;
      background: #fff;
      border: 1px #e8e8e8 solid;
      margin-top: 15px;
      margin-right: 10px;
      .shopRankingHead {
        background: #f9f9f9;
        color: #666;
        font-weight: bold;
        font-size: 14px;
        height: 33px;
        line-height: 33px;
        padding: 0 10px 1px;
      }
      ul {
        margin: 0;
        padding: 0;
        list-style: none;
        padding: 0 10px;
        li:last-child {
          border-bottom: none;
        }
        li:hover {
          cursor: pointer;
          .hoverModel {
            display: block;
          }
        }
        .hoverModel {
          position: absolute;
          right: 0;
          bottom: 10px;
          i {
            font-size: 24px;
            color: #df3033;
            border: .5px solid #ddd;
            border-radius: 2px;
            padding: 1px;
          }
          i:hover {
            cursor: pointer;
            color: #fff;
            background: #df3033;
            border-radius: 2px;
          }
          //display: none;
          .el-button {
            position: relative;
            top: 50%;
            transform: translateY(-50%);
          }
        }
        li {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          padding: 15px 0;
          border-bottom: 1px solid #f0f0f0;
          position: relative;
          .rankingRight {
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            align-items: flex-start;
            font-size: 12px;
            color: #666;
            margin-left: 10px;
            width: 130px;
            .rightTop, .rightBom {
              width: 100%;
              height: 18px;
              line-height: 18px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            .rightBom {
              color: #999;
            }
            .rankRigFont {
              color: #ea493d;
            }
          }
          .rankingLeft {
            img {
              width: 42px;
              height: 42px;
            }
          }
        }
      }
    }
    .shop {
      width: 80%;
      .shopHead {
        .shopHead-1 {
          color: #ff0000;
          font-size: 18px;
        }
        .shopHead-2 {
          margin-left: 30px;
          .el-input {
            width: 70px;
          }
        }
        font-size: 14px;
        padding: 11px 10px;
        display: flex;
      }
      ul {
        list-style: none;
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-start;
        align-items: center;
        padding: 0;
        margin: 0;
        li {
          width: 260px;
          max-width: 276px;
          border: 1px solid #eee;
          margin-bottom: 10px;
          margin-right: 10px;
          cursor: pointer;
        }
        li > .commodity:hover {
          .hoverRB {
            display: block;
          }
          img {
            opacity: 0.8;
          }
        }
        li > .commodity {
          display: flex;
          flex-direction: column;
          text-align: center;
          background: #fff;
          padding: 8px 8px 30px;
          border-radius: 3px;
          position: relative;
          .hoverRB {
            position: absolute;
            right: 10px;
            bottom: 10px;
            //display: none;
            transition: all 2s;

            i {
              font-size: 24px;
              color: #df3033;
              border: .5px solid #ddd;
              border-radius: 2px;
              padding: 1px;
            }
            i:hover {
              cursor: pointer;
              color: #fff;
              background: #df3033;
              border-radius: 2px;
            }
          }
          .commodimg {
            width: 240px;
            height: 240px;
            display: flex;
            justify-content: center;
            align-items: center;
            img {
              width: auto;
              height: auto;
              max-width: 240px;
              max-height: 240px;
            }
          }
          .el-carousel__item {
            margin: 0 20px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            img {
              border: 1px solid #eee;
              border-radius: 3px;
              width: 30px;
              height: 2.76vh;
              min-height: 2.76vh;
              max-height: 2.76vh;
              margin: 4px 3px 0;
            }
            img:hover {
              cursor: pointer;
            }
          }
          .isThumb {
            border: 2px solid red !important;
          }
          .el-carousel__arrow {
            width: 14px;
            height: 14px;
            cursor: default;
            background-color: #b8b2b1;
          }
          .el-carousel__arrow--left {
            left: 0;
          }
          .el-carousel__arrow--right {
            right: 0;
          }
          .cont1 {
            text-align: left;
            color: #666;
            font-size: 12px;
            line-height: 1.5;
            padding: 0 3px;
          }
          .cont {
            width: 100%;
            height: 36px;
            font-size: 12px;
            line-height: 1.5;
            text-align: left;
            color: #666;
            transition: color 0.2s ease;
            word-break: break-all;
            overflow: hidden;
            text-overflow: ellipsis;
            //white-space: nowrap;
            //-webkit-line-clamo: 2;
            //-webkit-box-orient: vertical;
            padding: 0 3px;
          }
          .footer {
            text-align: left;
            font-size: 12px;
            line-height: 1.8;
            span:nth-child(1) {
              font-size: 14px;
              color: #999;
            }
            span:nth-child(2) {
              color: #c49173;
              font-weight: bold;
            }
          }
          .price {
            display: flex;
            justify-content: space-between;
            margin-top: 0;
            font-size: 14px;
            line-height: 20px;
            text-align: left;
            color: #cc0000;
            font-weight: 700;
            font-family: arial, sans-serif;
            padding: 0 3px;
            margin-bottom: 20px;
            .price1 {
              span {
                vertical-align: middle;
                font-family: MicrosoftYahei-regular, Arial, Helvetica,
                sans-serif;
              }
            }
            .price2 {
              i {
                padding: 3px;
                border-radius: 2px;
                font-size: 20px;
                font-weight: 600;
                transition: all 0.01s;
              }
              i:hover {
                cursor: pointer;
                color: #fff;
                background: #e1251b;
              }
            }
          }
        }
      }
    }
  }
  .fixRight {
    position: fixed;
    right: 5px;
    top: 50%;
    div {
      width: 60px;
      max-height: 60px;
      background: #FFFFFF;
      text-align: center;
      padding: 10px 0;
      color: #333;
      border-radius: 2px;
      transition: all 300ms;
      cursor: pointer;
      margin-bottom: 2px;
      //font-size: 12px;
      font: 12px/1.5 Microsoft YaHei,Heiti SC,tahoma,arial,Hiragino Sans GB,"\5B8B\4F53",sans-serif;
      i {
        font-size: 24px;
      }
    }
    div:hover {
      background: #E2331A;
      color: #FFF;
    }
    .goTop {
      color: #E1251B;
      .span {
        margin-top: 5px;
      }
    }
  }
}
</style>
